<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form v-loading="formLoading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="所属基地" prop="deptId">
      <el-input v-model="dataForm.deptName" v-popover:deptListPopover readonly="true" placeholder="所属基地"></el-input>
        <el-popover ref="deptListPopover" placement="bottom-start" trigger="click">
          <el-tree
            ref="deptListTree" node-key="id" 
            show-checkbox
            :data="deptList" 
            :props="deptListTreeProps"
            @check-change="checkChange"
            :check-strictly="true" 
            :default-expand-all="true"
            :highlight-current="true" 
            :expand-on-click-node="false"
          >
          </el-tree>
      </el-popover>
    </el-form-item>
    <el-form-item label="公告标题" prop="name">
      <el-input v-model="dataForm.name" placeholder="公告标题"></el-input>
    </el-form-item>
    <el-form-item label="公告内容" prop="content">
      <el-input 
        type="textarea"
        :rows="3"
        placeholder="请输入内容"
        v-model="dataForm.content">
      </el-input>
    </el-form-item>
    <el-form-item label="文件" prop="fileIds">
      <div >
        <multipleFileUpload ref="multipleFileUploadRef"></multipleFileUpload>
        <!--<el-input v-model="dataForm.liveImage" placeholder="现场图片路径"></el-input>-->
      </div>
     <!--<el-input v-model="dataForm.fileIds" placeholder="文件" @change="getFile(dataForm.fileIds)"></el-input>-->
    </el-form-item>
    <el-form-item label="状态" size="mini" prop="status">
      <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">下架</el-radio>
          <el-radio :label="0">上架</el-radio>
      </el-radio-group>    
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button  v-loading="formLoading" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import multipleFileUpload from '@/components/multiple-flie-upload/index'
import { treeDataTranslate } from '@/utils'
  export default {
    components: {
      multipleFileUpload
    },
    data () {
      return {
        visible: false,
        formLoading: false,
        dataForm: {
          id: 0,
          deptId: '',
          deptName:'',
          name: '',
          content: '',
          status: 0,
          fileIds: '',
        },
        dataRule: {
          deptId: [
            { required: true, message: '所属基地不能为空', trigger: 'change' }
          ],
          name: [
            { required: true, message: '公告标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '公告内容不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
         
        },
        deptList: [],
        deptListTreeProps: {
          label: 'name',
          children: 'children',
          disabled: function(data, node){
            return data.type == 1 ? false :true
          }
        },
        tableData: [] 
      }
    },
    methods: {
      init (id) {
        if(id==null||id==''||id==undefined){
          this.dataForm.deptId = null
          this.dataForm.deptName = null
        }
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/sys/dept/select2'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.deptList = treeDataTranslate(data.deptList, 'id')
        }).then(() => {
             this.visible = true
             this.formLoading = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/buss/busspublicnotice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deptId = data.bussPublicNotice.deptId
                this.dataForm.name = data.bussPublicNotice.name
                this.dataForm.content = data.bussPublicNotice.content
                this.dataForm.status = data.bussPublicNotice.status
                this.dataForm.fileIds = data.bussPublicNotice.fileIds
                this.dataForm.version = data.bussPublicNotice.version
                this.deptListTreeSetCurrentNode()
                this.getFile(this.dataForm.fileIds)
              }
            })
          }
        })

        })

     
      },
      // 表单提交
      dataFormSubmit () {
         this.$refs['dataForm'].validate((valid) => {
          if (valid && !this.formLoading) {
            var uploadFileIds = this.$cookie.get('uploadFileIds')
            this.formLoading = true

            this.$http({
            url: this.$http.adornUrl(`/buss/busspublicnotice/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'deptId': this.dataForm.deptId,
              'name': this.dataForm.name,
              'content': this.dataForm.content,
              'status': this.dataForm.status,
              'version': this.dataForm.version,
              'fileIds': this.$cookie.get('uploadFileIds'),
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
                // 请求完成后 删除上传的id
                this.$refs['multipleFileUploadRef'].clearHistory()
              } else {
                this.formLoading = false
                this.$message.error(data.msg)
              }
            })
          }
         })
      },
        // 菜单树设置当前选中节点
      deptListTreeSetCurrentNode () {
        this.$refs.deptListTree.setChecked(this.dataForm.deptId,true,false)
        this.dataForm.deptName = (this.$refs.deptListTree.getCheckedNodes() || [])[0].name
      },
        // 节点修改
      checkChange(data, checked, node){
          if(checked == true){// 选中部门
              this.checkedId = data.id;
              this.$refs.deptListTree.setCheckedNodes([data])
              this.dataForm.deptId = data.id
              this.dataForm.deptName = data.name
          }
      },
    }
  }
</script>
